package com.sinocare.project.maintenance.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.common.entity.SccMenu;
import com.sinocare.project.maintenance.service.SccRoleService;
import com.sinocare.project.common.entity.SccRole;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 角色表(SccRole)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:53:45
 */
@RestController
@RequestMapping("sccRole")
@CrossOrigin
public class SccRoleController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccRoleService sccRoleService;

    /**
     * 查询所有数据
     *
     *
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll() {
        List<SccRole> list = this.sccRoleService.list();
        return Result.ok(ResultCode.SUCCESS,list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS,this.sccRoleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sccRole 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SccRole sccRole) {
        sccRole.setStatus(1);
        sccRole.setCreateTime(DateUtil.date());
        sccRole.setUpdateTime(DateUtil.date());
        return Result.ok(ResultCode.SUCCESS,this.sccRoleService.save(sccRole));
    }

    /**
     * 修改数据
     *
     * @param sccRole 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SccRole sccRole) {
        UpdateWrapper<SccRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id",sccRole.getRoleId());
        wrapper.set(!StrUtil.hasBlank(sccRole.getRoleName()),"role_name",sccRole.getRoleName());
        wrapper.set(!StrUtil.hasBlank(sccRole.getRoleDesc()),"role_desc",sccRole.getRoleDesc());
        wrapper.set(!StrUtil.hasBlank(sccRole.getRemark()),"remark",sccRole.getRemark());
        wrapper.set("status",sccRole.getStatus());
        wrapper.set("update_time",DateUtil.date());
        boolean update = this.sccRoleService.update(wrapper);
        return Result.ok(ResultCode.SUCCESS,update);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS,this.sccRoleService.removeByIds(idList));
    }
}

